package com.android.happymall.utils.update;

import android.app.DialogFragment;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.android.happymall.utils.ConstantsUrl;
import com.android.happymall.utils.HttpUtils;
import com.android.happymall.utils.MyLog;
import com.android.happymall.utils.ObjectUtils;
import com.android.happymall.R;

import okhttp3.Call;


public class UpVersionDialog extends DialogFragment implements View.OnClickListener {
    private static final String ARG_PARAM1 = "param1";


    private UpVersion upVersion;

    private OnUpVersionListener mListener;

    private TextView tv_version_name;
    private TextView tv_version_content;


    public UpVersionDialog() {
    }

    public static UpVersionDialog newInstance(UpVersion upVersion) {
        UpVersionDialog fragment = new UpVersionDialog();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM1, upVersion);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            upVersion = (UpVersion) getArguments().getSerializable(ARG_PARAM1);
            //通过是否为强制升级设置DialogFragment交互状态
//            if ("1".equals(upVersion.version_isUp)) {

//            } else {
//            setCancelable(true);
//            }
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCancelable(false);
        View layout = inflater.inflate(R.layout.dialog_fragment_up_version, container, false);
        tv_version_name = (TextView) layout.findViewById(R.id.tv_version_name);
        tv_version_content = (TextView) layout.findViewById(R.id.tv_version_content);
        layout.findViewById(R.id.tv_cancel).setOnClickListener(this);
        layout.findViewById(R.id.tv_yes).setOnClickListener(this);
        if (ObjectUtils.isNetworkConnected(getActivity())) {
            Update();
        } else {
            ObjectUtils.toast(getActivity(), "网络异常~~");
            getDialog().dismiss();
        }


        return layout;
    }

    public void onButtonPressed(String path) {
        if (mListener != null) {
            mListener.onUpVersion(path);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnUpVersionListener) {
            mListener = (OnUpVersionListener) context;
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                if (upVersion != null) {
//                    if ("1".equals(upVersion.version_isUp)) {
//                        System.exit(0);
//                    } else {
                    getDialog().dismiss();
//                    }
                }
                break;
            case R.id.tv_yes:
                ObjectUtils.toast(getActivity(), "正在检测最新版本...");
                if (upVersion != null) {
//                    if ("1".equals(upVersion.version_isUp)) {
//                        startDownloadApk();
//                        tv_yes.setEnabled(false);
//                        tv_yes.setText("下载中");
//                    } else {
                    startDownloadApk();
                    getDialog().dismiss();
//                    }

                }
                break;
        }
    }


    private void Update() {


        String address = ConstantsUrl.domain + ConstantsUrl.MAIN_UPDATE_VERSION;
        MyLog.e("地址", address);
        HttpUtils.get(address, null, null, new DownloadCallback() {

            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(UpVersion response, int id) {
                upVersion = response;
                if (response.getCode() == 0) {
                    if (ObjectUtils.getVersionCode(getActivity()) < Integer.parseInt(response.getData().getName())) {
                        tv_version_content.setText(response.getData().getContent());
                        tv_version_name.setText("V(" + response.getData().getVersion() + ")");
                    } else {
                        ObjectUtils.toast(getActivity(), "已经是最新版本");//"未检测到最新版本");
                        getDialog().dismiss();
                    }
                } else {
                    ObjectUtils.toast(getActivity(), "已经是最新版本");//"未检测到最新版本");
                    getDialog().dismiss();
                }
            }
        });


    }

    private void startDownloadApk() {
        Intent intent = new Intent(getActivity().getApplicationContext(), DownloadService.class);
        intent.putExtra("update", upVersion.getData().getDown_url());
        Log.e("version_url", " upVersion.version_url");
//                  intent.putExtra(Const.APK_DOWNLOAD_URL, "http://7xnvdy.com1.z0.glb.clouddn.com/aq360release");
        getActivity().startService(intent);
        ObjectUtils.toast(getActivity(), "正在下载，请在通知栏查看下载进度");
    }




    public interface OnUpVersionListener {
        void onUpVersion(String path);
    }
}
